<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Shijue_qj extends CI_Controller {

	public function __construct() {
		parent::__construct();
		//加载队列
		//$this->load->model('queue_model');
		error_reporting(E_ERROR); 

		ini_set("display_errors","Off");
	}


	public function get_list(){

		$get_task = "http://eagle.elephant.vcg.com/api/spider-tasks?quanjing=1";
		$ll = $this->send_http($get_task);
		$list_task = json_decode($ll,true);
		if($list_task['status'] == 400){
			echo "###############";
			sleep(30);
			return false;
		}

		//print_r($list_task);	
		$task_url = "http://eagle.elephant.vcg.com/api/spider-get-quanjing-by-task-id?task_id=".$list_task['data'][0]['task_id']."&page=1";
		$task = $this->send_http($task_url);
		$task_list_json = json_decode($task,true);
		$task_list = $task_list_json['data'];
		//print_r($task_list);
		$toutaloage = $task_list_json['totalPage'];
		echo $toutaloage . "\n";
		$page = 1;
		while($page <= $toutaloage){
			$task_url = "http://eagle.elephant.vcg.com/api/spider-get-quanjing-by-task-id?task_id=".$list_task['data'][0]['task_id']."&page=".$page;
			$task = $this->send_http($task_url);
			$task_list_json = json_decode($task,true);
			$task_list = $task_list_json['data'];
			#print_r($task_list);
			foreach($task_list as $v){
				$data = [];
				$str = "";
				//sleep(1);
				#$v['image_id'] = strtolower($v['image_id']);
				$url = "http://www.quanjing.com/imgbuy/".$v['image_id'].".html";
				echo $url . "\n";
				$rs = $this->send_http($url);
				#var_dump($rs);
				#echo "\n";
				#echo $rs;exit;
				if( preg_match_all('|var imgurl = "(.*?)"|i', $rs, $out)) {

					$str = $out[1][0];
                			#print_r($out);
				}
				if( preg_match_all('|<img src="(.*?)" alt=".*?" />|i', $rs, $out)) {

					$str = $out[1][0];
                			#print_r($out);
				}
				if( preg_match_all('|<img id="Img1"  src="(.*?)" />|i', $rs, $out)) {

					$str = $out[1][0];
                                	#print_r($out);
				}

				#print_r($str);	#print_r($img_list);
        			#var_dump( $img_list['imglist'][0]['imgurl']);
				if($str == false){
					$up_dat = "http://eagle.elephant.vcg.com/api/spider-update-quanjing-clue?id=".$v['id']."&status=2";
					$sa = $this->send_http($up_dat);
					$tt['pic_id'] = $v['id'];
					echo "\n";
					echo $v['image_id'];
					echo "\n";
					echo  "############################";
					#sleep(1);
				}else{

					$data['pid'] = rand(100000000000,100000000000000000);
					$data['url'] = $url;
					$data['pic_url'] = $str ? $str : '';
					$data['is_cover'] =0;
					$data['clue_name'] = 'quanjing';
					$data['url_article_title'] = '';
					$data['url_article'] = $str;
					$data['client_date'] = '';
					$data['company_id'] = $v['company_id'];
					$data['task_id'] = $v['task_id'];
					$data['clue_id'] = $v['clue_id'];
					#print_r($data);
					$res_data = json_encode($data);
					//print_r( $img_list['imglist'][0]['imgurl']);
					$post ='['.$res_data.']';
					$post_url = 'http://shijue.qingapi.cn/task/start';
					$rr = $this -> send_http($post_url,array('resource'=>$post));
					//print_r($rr);
					var_dump($rr);
					#echo $rr;
					echo "\n";
					echo "+++++++++++++++++++++++++++++++++\n";
					#sleep(1);
				}
				

			}
			$page++;
		}
		$up_task = "http://eagle.elephant.vcg.com/api/spider-update-quanjing-task?task_id=".$list_task['data'][0]['task_id'];
		#sleep(200);
		$rrrr = $this->send_http($up_task);
		echo $rrrr . "\n";
	} 



	function send_http($url, $post = array(), $ip = "", $timeout = 60, $header = array(), $connecttimeout = 60) {

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36');
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

		if($post){
			if(is_array($post)){
				$post = http_build_query($post);
			}
			//echo $post;
			//echo "\n";
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}

		if ($header){
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		$rs = curl_exec($ch);
		$http_info = curl_getinfo($ch);
		#print_r($http_info);	
		curl_close($ch);
		if($http_info['http_code'] != 200 && $http_info['http_code'] != 302 && $http_info['http_code'] != 301 ){
			return false;
		}
		return $rs;
	}

	function test(){
		echo $this->send_http("https://www.quanjing.com/imgbuy/ul0081-8610.html");
	}

}
